home *** CD-ROM | disk | FTP | other *** search
/ Champak 132 (Alt) / Vol 132.iso / games / cat_with.swf / scripts / __Packages / Vec.as < prev    next >
Encoding:
Text File  |  2011-06-09  |  2.8 KB  |  119 lines

  1. class Vec
  2. {
  3.    var x;
  4.    var y;
  5.    var px;
  6.    var py;
  7.    var vx;
  8.    var vy;
  9.    var links;
  10.    var distances;
  11.    function Vec(xx, yy)
  12.    {
  13.       this.x = xx;
  14.       this.y = yy;
  15.       this.px = xx;
  16.       this.py = yy;
  17.       this.vx = 0;
  18.       this.vy = 0;
  19.       this.links = [];
  20.       this.distances = [];
  21.    }
  22.    function addLink(other)
  23.    {
  24.       if(Math.random() < 0.5)
  25.       {
  26.          this.links.push(other);
  27.          this.distances.push(this.distance(other));
  28.       }
  29.       else
  30.       {
  31.          this.links.unshift(other);
  32.          this.distances.unshift(this.distance(other));
  33.       }
  34.    }
  35.    function clearLinks()
  36.    {
  37.       this.links = [];
  38.       this.distances = [];
  39.    }
  40.    function update(o)
  41.    {
  42.       this.vx = this.x - this.px;
  43.       this.vy = this.y - this.py;
  44.       this.px = this.x;
  45.       this.py = this.y;
  46.       this.vx *= o;
  47.       this.vy *= o;
  48.       this.x += this.vx;
  49.       this.y += this.vy;
  50.    }
  51.    function distance(other)
  52.    {
  53.       var _loc3_ = this.x - other.x;
  54.       var _loc2_ = this.y - other.y;
  55.       return Math.sqrt(_loc3_ * _loc3_ + _loc2_ * _loc2_);
  56.    }
  57.    function adjustUnit(other, distance)
  58.    {
  59.       var _loc2_ = other.x - this.x;
  60.       var _loc3_ = other.y - this.y;
  61.       var _loc4_ = Math.sqrt(_loc2_ * _loc2_ + _loc3_ * _loc3_);
  62.       var _loc5_ = distance - _loc4_;
  63.       if(_loc4_ == 0)
  64.       {
  65.          return undefined;
  66.       }
  67.       _loc2_ = _loc2_ / _loc4_ * (_loc5_ * 0.5);
  68.       _loc3_ = _loc3_ / _loc4_ * (_loc5_ * 0.5);
  69.       this.x -= _loc2_;
  70.       this.y -= _loc3_;
  71.       other.x += _loc2_;
  72.       other.y += _loc3_;
  73.    }
  74.    function adjustUnitRate(other, distance, alpha)
  75.    {
  76.       var _loc2_ = other.x - this.x;
  77.       var _loc3_ = other.y - this.y;
  78.       var _loc5_ = Math.sqrt(_loc2_ * _loc2_ + _loc3_ * _loc3_);
  79.       var _loc4_ = distance - _loc5_;
  80.       if(_loc5_ == 0)
  81.       {
  82.          return undefined;
  83.       }
  84.       _loc2_ /= _loc5_;
  85.       _loc3_ /= _loc5_;
  86.       this.x -= _loc2_ * (_loc4_ * alpha);
  87.       this.y -= _loc3_ * (_loc4_ * alpha);
  88.       other.x += _loc2_ * (_loc4_ * (1 - alpha));
  89.       other.y += _loc3_ * (_loc4_ * (1 - alpha));
  90.    }
  91.    function adjustLinks()
  92.    {
  93.       var _loc2_ = 0;
  94.       while(_loc2_ < this.links.length)
  95.       {
  96.          this.adjustUnit(this.links[_loc2_],this.distances[_loc2_]);
  97.          _loc2_ = _loc2_ + 1;
  98.       }
  99.    }
  100.    function adjustLinks2(scale)
  101.    {
  102.       var _loc2_ = 0;
  103.       while(_loc2_ < this.links.length)
  104.       {
  105.          this.adjustUnit(this.links[_loc2_],this.distances[_loc2_] * scale);
  106.          _loc2_ = _loc2_ + 1;
  107.       }
  108.    }
  109.    function adjustLinks3(scale, alpha)
  110.    {
  111.       var _loc2_ = 0;
  112.       while(_loc2_ < this.links.length)
  113.       {
  114.          this.adjustUnitRate(this.links[_loc2_],this.distances[_loc2_] * scale,alpha);
  115.          _loc2_ = _loc2_ + 1;
  116.       }
  117.    }
  118. }
  119.